<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TaskRequest;
use App\Models\Item;

use DB;

class ItemController extends Controller
{
    public function index(Request $request)
    {
        $where = $param = [];
      
        $list = Item::where($where)
                        ->orderBy('created_at','desc')
                        ->paginate(20);
    	return view('admin.item.index',compact('list','param'));
    }

    public function show($id)
    {
    	$data = Task::find($id);
        $replies = TaskReply::where('task_id',$id)->paginate(5);
        //dd($replies);
    	return view('admin.task.show',compact('data','replies'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Item::find($id);
        return view('admin.item.edit',compact('data'));
    }

    

    public function create()
    {
        return view('admin.item.add');
    }

    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        $rs['status'] = 'danger';       
        $flag = Item::where('id',$id)->update($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';    
        }
        
        return redirect('zadmin/item')->withInput()->with('rs',$rs);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
       
        $rs   = Item::create($data);
        if($rs) {
            return redirect('zadmin/item');
        }
        return back();
    }

    
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = Item::destroy($id);
        if ($rs) {
            return redirect('zadmin/item');
        }
        return back();
    }
}
